using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Alexandrie.helper;
using Alexandrie.dal;
using Alexandrie.Model;
using System.Collections;

namespace Alexandrie
{
    public partial class AlexandrieInsertForm : Form
    {
        private System.ComponentModel.BackgroundWorker mListBooks;

        private IList<Book> mModel;

        private Book mSelectedBook;

        public Book selectedBook
        {
            get { return mSelectedBook; }
            set { mSelectedBook = value; }
        }

        public AlexandrieInsertForm()
        {
            InitializeComponent();

            foreach (Plugin.Store s in Enum.GetValues(typeof(Plugin.Store)))
            {
                cStoresCombo.Items.Add(Plugin.startStore.ToString() + s.ToString());
            }
            cStoresCombo.SelectedIndex = Int32.Parse(Plugin.plugin.getOptions().GetString("store", Plugin.Store.com.ToString()));

            using (mListBooks = new BackgroundWorker())
            {
                mListBooks.DoWork += new DoWorkEventHandler(doSearchBooks_doWork);
                mListBooks.RunWorkerCompleted += new RunWorkerCompletedEventHandler(doSearchBooks_workCompleted);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String lSearchText = cSearchValue.Text;

            cSearchValue.Enabled = false;
            cStoresCombo.Enabled = false;
            button1.Enabled = false;

            booksDataGrid.Rows.Clear();

            this.Cursor = Cursors.WaitCursor;

            mListBooks.RunWorkerAsync(lSearchText);
        }

        private void doSearchBooks_doWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            IBookSearch lSearchService = new BookSearch();
            IList<Book> lListBooks = lSearchService.getBooks((String)e.Argument);

            e.Result = lListBooks;
        }

        private void doSearchBooks_workCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                mModel = (IList<Book>)e.Result;
                foreach (Book b in mModel)
                {
                    booksDataGrid.Rows.Add(new Object[] { b.image, b.titleBook + " - " + b.author });
                }
            }
            else
            {
                //no book
            }

            cSearchValue.Enabled = true;
            cStoresCombo.Enabled = true;
            button1.Enabled = true;
            this.Cursor = Cursors.Default;
        }

        private void cStoresCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            Plugin.plugin.getOptions().SetString("store", cStoresCombo.SelectedIndex.ToString());
        }

        private void booksDataGrid_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.selectedBook = mModel[e.RowIndex];
            this.Close();
        }

    }
}